package com.tcgroup.phone.base_core.framework.mvvm;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;

import com.alibaba.android.arouter.launcher.ARouter;
import com.gyf.immersionbar.ImmersionBar;
import com.tcgroup.phone.base_core.R;
import com.tcgroup.phone.base_core.event.event_bus.BaseFragmentEvent;
import com.tcgroup.phone.base_core.framework.IBaseView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 作者: wangfei
 * 创建时期: 2020/3/18.
 * 包名: com.tcgroup.phone.base_core.framework.mvvm
 * 类名: OSAndroidMvvm
 */
public abstract class AbstractFragment extends Fragment implements IBaseView {

    protected AppCompatActivity mActivity;
    protected View mView;
    private ViewStub mViewStubToolbar;
    protected FrameLayout mViewStubContent;
    protected Toolbar mToolbar;
    protected View mStatusBarView;
    private TextView mToolbarTitle;
    private boolean isViewCreated = false;
    private boolean isViewVisable = false;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ARouter.getInstance().inject(this);
        EventBus.getDefault().register(this);
        mActivity = (AppCompatActivity) getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.core_fragment_root_01, container, false);
        initCommonView(mView);
        initView(mView);
        initListener();
        return mView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        isViewCreated = true;
        //如果启用了懒加载就进行懒加载，否则就进行预加载
        if (enableLazyData()) {
            lazyLoad();
        } else {
            initData();
        }
    }

    @Override // setUserVisibleHint只有结合FragmentPagerAdapter才能使用
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        isViewVisable = isVisibleToUser;
        //如果启用了懒加载就进行懒加载，
        if (enableLazyData() && isViewVisable) {
            lazyLoad();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        ImmersionBar.with(this).destroy(this);
    }

    //默认不启用懒加载
    public boolean enableLazyData() {
        return false;
    }

    private void lazyLoad() {
        //这里进行双重标记判断,必须确保onCreateView加载完毕且页面可见,才加载数据
        if (isViewCreated && isViewVisable) {
            initData();
            //数据加载完毕,恢复标记,防止重复加载
            isViewCreated = false;
            isViewVisable = false;
        }
    }

    protected void initCommonView(View view){
        mViewStubToolbar = view.findViewById(R.id.core_view_stub_toolbar);
        mViewStubContent = view.findViewById(R.id.core_view_stub_content);
        mStatusBarView = view.findViewById(R.id.status_bar_view);
        if (enableToolbar()){
            mViewStubToolbar.setLayoutResource(onBindToolbarLayout());
            View viewTooBbar = mViewStubToolbar.inflate();
            initTooBar(viewTooBbar);
        }
        initConentView(mViewStubContent);
    }

    public int onBindToolbarLayout() {
        return R.layout.core_common_toolbar;
    }

    public boolean enableToolbar() {
        return false;
    }

    protected void initTooBar(View view) {
        mToolbar = view.findViewById(R.id.core_toolbar_root);
        mToolbarTitle = view.findViewById(R.id.core_toolbar_title);
        if (mToolbar != null) {
            initImmersionBar(mToolbar);
            mActivity.setSupportActionBar(mToolbar);
            mActivity.getSupportActionBar().setDisplayShowTitleEnabled(false);
            mActivity.getSupportActionBar().setDisplayHomeAsUpEnabled(enable_TB_BackImage());
            mToolbar.setNavigationOnClickListener(v -> {
                mActivity.onBackPressed();
                mActivity.finish();
            });
        }
        if (mToolbarTitle != null) {
            mToolbarTitle.setText(getToolbarTitle());
        }
    }

    /**
     * 初始化沉浸式
     * Init immersion bar.
     */
    public void initImmersionBar(View view) {
        ImmersionBar.with(this).titleBar(mToolbar,false).transparentBar()
                .init();
    }

    public void initConentView(ViewGroup root){
        LayoutInflater.from(mActivity).inflate(onBindLayout(), root, true);
    }

    @Override
    public void initView() {

    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void finishActivity() {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void showContent() {

    }

    @Override
    public void showEmpty() {

    }

    @Override
    public void showError() {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public <T> void onEvent(BaseFragmentEvent<T> event) {
    }

    public boolean enable_TB_BackImage(){
        return true;
    }

    public abstract int onBindLayout();

    public abstract String getToolbarTitle();

    public abstract void initView(View view);
}
